package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.MemberLevel;
import org.millioncall.yueyoga.admin.service.MemberLevelService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class MemberLevelAction {

	private MemberLevelService memberLevelService;

	public MemberLevelService getMemberLevelService() {
		return memberLevelService;
	}

	public void setMemberLevelService(MemberLevelService memberLevelService) {
		this.memberLevelService = memberLevelService;
	}

	// 查询会员等级
	@RequestMapping(value = "/ajaxgetmemberlevelsinpage.do")
	public void getspeakersByPage(HttpServletRequest request, HttpServletResponse response) {
		String msg = null;

		int start = 0;
		int pageSize = 20;

		String strStart = request.getParameter("start");
		if(strStart != null) {
			start = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if(strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		PaginatedList<MemberLevel> p = null;
		p = memberLevelService.getMemberLevelInPage(start, pageSize);

		if(p == null || p.size() == 0) {
			msg = "{'success':true,'total':0,'items':[]}";
		} else {
			StringBuilder sb = new StringBuilder("[");
			int totalspeakers = p.getPager().getCountOfTotalResults().intValue();
			for(MemberLevel memberlevel : p) {
				sb.append("{");
				sb.append("'id':'" + memberlevel.getMemberLevelId() + "',");
				sb.append("'levelName':'" + memberlevel.getLevelName()+ "',");
				sb.append("'enable':'" + memberlevel.getEnable() + "'");
				sb.append("},");
			}
			String tmpspeakers = sb.toString();
			if(tmpspeakers.length() > 1) {
				tmpspeakers = tmpspeakers.substring(0, tmpspeakers.length() - 1);
			}
			tmpspeakers += "]";
			msg = "{'success':true,'total':" + totalspeakers + ",'items':" + tmpspeakers + "}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 添加会员等级
	@RequestMapping(value = "/ajaxaddmemberlevel.do")
	public void addmemberlevel(HttpServletRequest request, HttpServletResponse response) {
		String msg = null;
		String levelName = request.getParameter("levelName");
		if(StringUtils.isBlank(levelName)) {
			msg = "{'success':false,'msg':'会员等级性名不可为空'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		if(levelName.length() > 15) {
			msg = "{'success':false,'msg':'会员等级姓名长度应介于2-15之间'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("levelName", levelName);
		map.put("enable", Boolean.parseBoolean(request.getParameter("enable")));
		if(memberLevelService.saveMemberLevel(map)) {
			msg = "{'success':true,'msg':'添加会员等级成功'}";
		} else {
			msg = "{'success':false,'msg':'添加会员等级失败，请稍候重试或联系管理员'}";
		}
		
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 编辑会员等级
	@RequestMapping(value = "/ajaxeditmemberlevel.do")
	public void editmemberlevel(HttpServletRequest request, HttpServletResponse response) {
		Integer id = Integer.parseInt(request.getParameter("memberLevelId"));
		String msg = null;
		String levelName = request.getParameter("levelName");
		if(StringUtils.isBlank(levelName)) {
			msg = "{'success':false,'msg':'会员等级名不可为空'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		if(levelName.length() > 15) {
			msg = "{'success':false,'msg':'会员等级姓名长度应介于2-15之间'}";
			WebAppUtils.ajaxResponse(response, msg);
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberLevelId", id);
		map.put("levelName", levelName);
		map.put("enable", Boolean.parseBoolean(request.getParameter("enable")));
		
		if(memberLevelService.updateMemberLevel(map)) {
			msg = "{'success':true,'msg':'更新会员等级成功'}";
		} else {
			msg = "{'success':false,'msg':'更新会员等级失败，请稍候重试或联系管理员'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
	// 删除会员等级
	@RequestMapping(value = "/ajaxdeletememberlevel.do")
	public void deletememberlevel(HttpServletRequest request, HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if(StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除会员等级的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids=new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if(arrids[i]!=null&&!arrids[i].equals("")){
					deids[i]=Integer.parseInt(arrids[i]);
				}
			}
			try {
				memberLevelService.deleteMemberLevelByIds(deids);
				msg = "{'success':true,'msg':'删除会员等级成功'}";
			} catch (RuntimeException re) {
				msg = "{'success':false,'msg':'删除会员等级失败，请稍候重试或联系管理员'}";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
}
